#include "Utils.h"
#include <io.h>


void drawTorus(float R, float r, int N, int n,int angle)
{
    int maxn= 1000; // max precision
    float cosv,sinv,cosw,sinw,cosvdv,sinvdv,coswdw,sinwdw;
    float  reduction = angle/180.0;
    n=min(n,maxn-1);
    N=min(N,maxn-1);
    float rr=1.5f*r;
    double dv=2*M_PI/n;
    double dw=2*M_PI/N;
    double v=0.0f;
    double w=0.0f;
// outer loop
//   printf("\ndw = %f\n",dw);
    while(w<reduction*M_PI+dw)
    {
        v=0.0f;
        glBegin(GL_TRIANGLE_STRIP);
        // inner loop
        while(v<2*M_PI+dv)
        {
            cosv = cos(v);sinv = sin(v);
            cosw = cos(w);sinw = sin(w);
            cosvdv = cos(v+dv);sinvdv = sin(v+dv);
            coswdw = cos(w+dw);sinwdw = sin(w+dw);

            glNormal3d(
                (R+rr*cosv)*cosw-(R+r*cosv)*cosw,
                (R+rr*cosv)*sinw-(R+r*cosv)*sinw,
                (rr*sinv-r*sinv));
            glVertex3d((R+r*cosv)*cosw,
                          (R+r*cosv)*sinw,
                          r*sinv);
            glNormal3d(
                (R+rr*cosvdv)*coswdw-(R+r*cosvdv)*coswdw,
                (R+rr*cosvdv)*sinwdw-(R+r*cosvdv)*sinwdw,
                rr*sin(v+dv)-r*sin(v+dv));
            glVertex3d((R+r*cosvdv)*coswdw,
                          (R+r*cosvdv)*sinwdw,
                          r*sin(v+dv));
            v+=dv;
        } // inner loop
        glEnd();
        w+=dw;
    } //outer loop


}
